package com.sqstudio.astar
{
	/**
	 * A*寻路
	 * 
	 * 
	 * 
	 * <br>
	 * 网格设定: 1.没有行列的概念， 只有 X方法网格数量 和Y方法网格数量    ，二维数组 与地图一致
	 * <br>2.单个节点Node的x和y 是 真实的x，y ，取此节点 用 nodeArr[node.y][node.x] 即可找到
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 */
	public class AStar 
	{
		private var _grid:Grid;
		private var _endNode:Node;
		private var _startNode:Node;
		private var _path:Array;		
		
		public function AStar()
		{
		}
		
		public function findPath(grid:Grid):Boolean
		{
			_grid = grid;
			
			_startNode = _grid.startNode;
			_endNode = _grid.endNode;
			
			return search();
		}
		
		public function search():Boolean
		{
			var node:Node = _startNode;
			var pf:PathFinder = new PathFinder(_grid.numY,_grid.numX);
			pf.nodeArr = _grid.nodes;
			var pathArr:Array = pf.findPath(_startNode.x,_startNode.y,_endNode.x,_endNode.y);
			if(pathArr == null) return false;
			buildPath(pathArr);
			return true;
		}
		
		private function buildPath(arr:Array):void
		{
			_path = new Array();
			var len:int = arr.length;
			for(var i:int = 0 ; i < len ; i++)
			{
				_path.push(_grid.getNode(arr[i][0],arr[i][1]));
			}
		}
		
		public function get path():Array
		{
			return _path;
		}		
		
		public function get visited():Array
		{
			return new Array();
		}
	}
}
